package com.sundaying.gooway.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.sundaying.gooway.dao.UrlClassDao;
import com.sundaying.gooway.entity.UrlClass;
import com.sundaying.gooway.entity.UrlItem;

@Component
public class UrlClassService {
	private UrlClassDao urlClassDao;

	public UrlClassDao getUrlClassDao() {
		return urlClassDao;
	}

	@Resource
	public void setUrlClassDao(UrlClassDao urlClassDao) {
		this.urlClassDao = urlClassDao;
	}

	public int add(UrlClass urlClass) {
		int result = 0;
		result = urlClassDao.save(urlClass);
		return result;
	}

	public List<UrlClass> list() {
		List<UrlClass> urlClassList = urlClassDao.list();
		return urlClassList;
	}
	
	public List<UrlClass> list(int page, int rows, String sort, String order, UrlClass urlClass) {
		String hql = "from UrlClass urlClass where 1=1 ";
		List<Object> param = new ArrayList<Object>();
		hql = addWhere(hql, urlClass, param);
		// 排序
		if(!sort.equals("")){
			hql += " order by " + sort;
			
			if(!order.toLowerCase().equals("asc")){
				hql += " desc";
			}
		}
		List<UrlClass> urlClassList = urlClassDao.list(hql,param,page,rows);
		return urlClassList;
	}

	public long total(UrlClass urlClass) {
		long result = 0;
		String hql = "select count(*) from UrlClass urlClass where 1=1 ";
		List<Object> param = new ArrayList<Object>();
		hql = addWhere(hql, urlClass, param);
		result = urlClassDao.count(hql, param);
		return result;
	}
	
	public int update(UrlClass urlClass) {
		int result = 0;
		result = urlClassDao.update(urlClass);
		return result;
	}

	public int delete(int id) {
		int result = 0;
		System.out.println(id);
		result = urlClassDao.delete(urlClassDao.load(id));
		return result;
	}

	public List<UrlClass> loadByParentId(int id) {
		String hql = "from UrlClass urlClass where urlClass.parentId=?";
		List<Object> param = new ArrayList<Object>();
		param.add(id);
		List<UrlClass> urlClassList = urlClassDao.list(hql, param);
		return urlClassList;
	}

	/**
	 * 获取所有网址类别集合对象，使用Tree形展示
	 * 
	 * @return List 网址类别集合对象
	 */
	public List<Map<String, Object>> tree(int classID) {
		List<UrlClass> nodes = null;
		nodes = this.loadByParentId(classID);

		List<Map<String, Object>> urlClassList = new ArrayList();
		for (UrlClass urlClass : nodes) {
			Map<String, Object> urlClassItem = new HashMap<String, Object>();
			urlClassItem.put("id", urlClass.getId());
			urlClassItem.put("text", urlClass.getName());
			urlClassItem.put("attributes", urlClass);

			if (this.hasChild(urlClass.getId())) {
				urlClassItem.put("state", "closed");
				urlClassItem.put("children", this.tree(urlClass.getId()));
			}

			urlClassList.add(urlClassItem);
		}

		return urlClassList;
	}

	/**
	 * 当前节点是否有子节点
	 * @param id
	 * 当前节点Id
	 * @return 是否存在子节点
	 */
	private boolean hasChild(int id) {
		boolean result = false;
		String hql = "from UrlClass urlClass where urlClass.parentId=?";
		List<Object> param = new ArrayList<Object>();
		param.add(id);
		
		if (urlClassDao.list(hql, param).size() > 0)
			result = true;
		return result;
	}
	
	private String addWhere(String hql, UrlClass urlClass, List<Object> param) {
		if (urlClass.getName() != null && !urlClass.getName().trim().equals("")) {
			hql += " and urlClass.name like '%%" + urlClass.getName().trim()
					+ "%%' ";
		}
		if (urlClass.getId() > 0) {
			hql += " and urlClass.Id=? ";
			param.add(urlClass.getId());
		}
		return hql;
	}
}
